package cn.xiaozhu.service.spring;
import cn.xiaozhu.dao.IFileMapper;
import cn.xiaozhu.dao.IUserMapper;
import cn.xiaozhu.entity.Resume;
import cn.xiaozhu.entity.User;
import cn.xiaozhu.service.facade.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.List;


/**
 * 用户服务类
 * @author shkstart
 * @date 2019/8/16 - 8:55
 */
@Service
public class UserService implements IUserService {

    private final static Logger logger = LoggerFactory.getLogger(UserService.class);
    /**
     * 访问dao层
     */
    @Autowired
    private IUserMapper userMapper;
    @Autowired
    private IFileMapper fileMapper;




    /**
     * 通过查询用户名来进行用户登陆
     *
     * @param
     * @param password
     * @return
     */
    @Override
    public boolean login(String userName, String password) {
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            return false;
        }
        User user = new User();
        user.setUserName(userName);
        user = userMapper.getUserByuserName(userName);
        if (user == null) {
            return false;
        } else {
            String passwordTemp = user.getPassWord();
            if (password.equals(passwordTemp)) {
                return true;
            } else {
                return false;
            }
        }
    }




    /**
     * 添加用户
     *
     * @param userName
     * @param password
     * @param email
     * @param phoneNumber
     * @return
     */

    private boolean newUser(String userName, String password, String email, String phoneNumber) {
        if (userName != null && password != null && email != null && phoneNumber != null) {
            //todo:需要增加异常处理，判断是否添加用户成功
            userMapper.addUser(userName, password, email, phoneNumber);
            return true;
        } else {
            return false;
        }
    }




    /**
     * 添加用户
     * @param userName
     * @param password
     * @param email
     * @param phoneNumber
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addUser(String userName, String password, String email, String phoneNumber) throws Exception{
       try {
           userMapper.addUser(userName, password, email, phoneNumber);
       }catch (Exception e){
         logger.error("dskljfkasjdf");
         throw new Exception("hghhhggh");
       }
           return true;
    }




    /**
     * 删除用户
     * 一、根据用户Id查找用户文件userFile，
     * 二、使用for循环读出用户文件列表的每条记录pathName,
     * 三、获取每条文件对象中的地址realPath,
     * 四、将用户上传文件删除，
     * 五、删除文件记录
     * 六、删除用户
     * userFile:存放通过用户Id查找到的文件List容器(容器当中文件类型为文件原有类型Resume)
     * pathName:通过List中的get()方法获取容器中的每一个Resume对象
     * realPath:调用resume实体类中的getRealPath()方法获取文件地址
     * @param Id
     * @return
     */
    @Override
    public boolean deleteUser(Integer Id) {
        if (Id != 0) {
            List<Resume> userFile = fileMapper.getFileList(Id);
            for (int i = 0; i < userFile.size(); i++) {
                Resume pathName= userFile.get(i);
                String realPath = pathName.getRealPath();
                File file = new File(realPath);
                    if (!file.exists()) {
                        logger.info("文件不存在");
                    } else {
                        file.delete();
                    }
                      fileMapper.delFile(Id);
            }
                      userMapper.delUser(Id);
                       return true;
            }else{
                return false;
        }

    }




    /**
     * 更新用户信息
     * @param Id
     * @param userName
     * @param password
     * @param email
     * @param phoneNumber
     * @return
     */
    @Override
    public boolean updateUser(Integer Id, String userName, String password, String email, String phoneNumber) {
        if (!StringUtils.isEmpty(Id)) {
            return userMapper.updateUser(Id, userName, password, email, phoneNumber);
        } else {
            return false;
        }
    }





    /**
     * 根据Id查询用户信息
     * @param Id
     * @return
     */
    @Override
    public User selectUser(Integer Id) {

        return userMapper.getUserById(Id);
    }





    /**
     * 查询返回所有用户信息
     *
     * @return
     */
    @Override
    public List<User> findAllUsers() {
        return userMapper.getUserList();
    }

}
